package cn.cloud9;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

public class JedisUtil {

    private JedisUtil() {}

    private static JedisPool pool;

    static {
        /**
         * 硬编码方式初始化
         * JedisPoolConfig jpc = new JedisPoolConfig();
         * jpc.setMaxTotal(128);
         * jpc.setMaxIdle(12);
         * String host = "192.168.2.225";
         * int port = 6379;
         * pool = new JedisPool(jpc, host, port);
         */

        ResourceBundle rb = ResourceBundle.getBundle("redis"); // redis.properties
        final String HOST = rb.getString("redis.host");
        final int PORT = Integer.parseInt(rb.getString("redis.port"));
        final int MAX_TOTAL = Integer.parseInt(rb.getString("redis.maxTotal"));
        final int MAX_IDLE = Integer.parseInt(rb.getString("redis.maxIdle"));

        JedisPoolConfig jpc = new JedisPoolConfig();
        jpc.setMaxTotal(MAX_TOTAL);
        jpc.setMaxIdle(MAX_IDLE);
        pool = new JedisPool(jpc, HOST, PORT);
    }

    public static Jedis getConnection() {
        return pool.getResource();
    }


}
